<?php
	class Url {
		private static $_urlFileUri = 'error_404.php';
		private static $_splittedDomainURI = array();
		private static $_hostName;
		private static $_subDomainValue;
		public static $registerVisit = true;
		public static $self = '/';
		private static $_pageNumber = 1;
		private static $_pageSlug = '';
		private static $_pageType = 'regular';
		private static $_adminPage = false;
		public static $pageVars = array();

		public static function processUrl() {
			self::$_urlFileUri = PHP_PATH . 'error_404.php';
			self::checkFiles();
			$hostName = str_replace( 'www.', '', $_SERVER[ 'HTTP_HOST' ] );
			$requestUri = explode( '?', $_SERVER[ 'REQUEST_URI' ] );
			$requestUri = trim( $requestUri[ 0 ], '/' );
			if( $requestUri == 'index.php') {
				header( 'Location: /' );
				exit;
			}

			$splittedHostName = explode( '.', $hostName );
			$hostLength = count( $splittedHostName );
			if( $hostLength > 2 ) {
				self::$_hostName = $splittedHostName[ $hostLength - 2 ] . '.' . $splittedHostName[ $hostLength - 1 ];
				self::$_subDomainValue = str_replace( '.' . self::$_hostName, '', $hostName );
			} else {
				self::$_hostName = $hostName;
				self::$_subDomainValue = "www";
			}
			
			if( stripos( $requestUri, 'nv/' ) !== false && stripos( $requestUri, '/nv/' ) === false ){
				$requestUri = '/' . $requestUri;
			}

			$tmpArrReqURI = $requestUri ? explode( '/', $requestUri ) : array();
			if( count( $tmpArrReqURI ) > 0 ){
				$firstURIValue = $tmpArrReqURI[0];
				if( $firstURIValue != '' ){
					/*$userCountObj = getMultiple( "user", "login||$firstURIValue", "count||Y" );
					if( $userCountObj->getTotalCount() > 0 )
					{
						$requestUri = 'profile/' . $requestUri;
					}*/
				}
			}

			$varsUri = explode( '/nv/', $requestUri );
			if( count( $varsUri ) > 1 ) {
				self::makePageVars( $varsUri[ 1 ] );
				$requestUri = $varsUri[ 0 ];
			} else {
				self::makePageVars( '' );
			}
			
			self::$self = $requestUri;
			self::$_splittedDomainURI = $splittedRequestUri = explode( '/', $requestUri );
			self::$_adminPage = self::$_splittedDomainURI[ 0 ] == ADMIN_URL_PART;
			if( self::$_adminPage ) {
				self::$_splittedDomainURI[ 0 ] = 'admin';
				$requestUri = str_replace( 'REPLACEHERE' . ADMIN_URL_PART, 'admin', 'REPLACEHERE' . $requestUri );
			} else if( self::$_splittedDomainURI[ 0 ] == 'admin' ) {
				self::$_splittedDomainURI[ 0 ] = ADMIN_URL_PART;
				$requestUri = str_replace( 'REPLACEHERE' . 'admin', ADMIN_URL_PART, 'REPLACEHERE' . $requestUri );
			}

			while( count( $splittedRequestUri ) > 0 && is_numeric( $splittedRequestUri[ count( $splittedRequestUri ) - 1 ] ) ) {
				self::$_pageNumber = array_pop( $splittedRequestUri );
				$requestUri = implode( '/', $splittedRequestUri );
			}

			if( $requestUri == '' ) {
				if( self::$_pageNumber == 1 ){
					self::$_urlFileUri = PHP_PATH . 'home.php';
				}						
			} else {
				$uriParts = explode( '/', $requestUri );
				$slug = $uriParts[ 0 ];
				if( file_exists( PHP_PATH . self::slugToName( $requestUri ) . '.php' ) ) {
					self::$_urlFileUri = PHP_PATH . self::slugToName( $requestUri ) . '.php';
				} else {
					$fileName = self::slugToName( $slug );
					if( file_exists( PHP_PATH . $fileName . '.php' ) ) {
						self::$_pageType = $slug;
						self::$_pageSlug = $uriParts[ 1 ];
						self::$_urlFileUri = PHP_PATH . $fileName . '.php';
					} else if( self::$_adminPage ) {
						if( $requestUri == 'admin' && file_exists( PHP_PATH . 'admin/' . self::getAdminDefaultPage() . '.php' ) ) {
							self::$_urlFileUri = PHP_PATH . 'admin/' . self::getAdminDefaultPage() . '.php';
						}
					}
				}
			}
		}

		private static function getAdminDefaultPage() {

			if( Login::checkLoginStatus() == Login::USER_LOGGED_IN ) {
				Login::getUserInfo( 'userRoleId' );
				$adminDefaultPages = array( '', 'requested_invite', '', 'requested_invite' );
				return $adminDefaultPages[ Login::getUserInfo( 'userRoleId' ) ];
			}
			return 'home';
		}

		private static function slugToName( $slug ) {
			return str_replace( '-', '_', $slug );
		}

		public static function __callStatic( $functionName, $argumentsList ) {
			$varName = '_' . lcfirst( str_replace( 'get', '', $functionName ) );
			if( isset( self::$$varName ) ) {
				return self::$$varName;
			} else {
				throw new Exception( "Call to undefined function $functionName!" );
			}
		}

		private function makePageVars( $varPart ) {
			self::$pageVars[ 'GETPOST' ] = array();
			self::$pageVars[ 'GET' ] = array();
			self::$pageVars[ 'POST' ] = array();
			self::$pageVars[ 'QUERY' ] = array();
			self::$pageVars[ 'COOKIE' ] = $_COOKIE;
			self::$pageVars[ 'FILES' ] = $_FILES;

			if( trim( $varPart ) != '' ) {
				$allVars = explode( '/', $varPart );

				if( ( count( $allVars ) % 2 ) !== 0 ) {
					self::$pageVars[ 'GETPOST' ][ 'ORPHAN' ] = trim( array_pop ( $allVars ) );
				}

				for( $varCount = 0; $varCount < count( $allVars ); $varCount += 2 ) {
					self::$pageVars[ 'GET' ][ trim( $allVars[ $varCount ] ) ] = stripslashes( stripslashes( urldecode( trim( $allVars[ $varCount + 1 ] ) ) ) );
					self::$pageVars[ 'QUERY' ][ trim( $allVars[ $varCount ] ) ] = stripslashes( stripslashes( urldecode( trim( $allVars[ $varCount + 1 ] ) ) ) );
					self::$pageVars[ 'GETPOST' ][ trim( $allVars[ $varCount ] ) ] = stripslashes( stripslashes( urldecode( trim( $allVars[ $varCount + 1 ] ) ) ) );
				}
			}

			foreach( $_GET as $key => $value ) {
				//$value = ( is_array( $value ) ) ? $value : filter_var($value, FILTER_SANITIZE_STRING);
				
				self::$pageVars[ 'GETPOST' ][ $key ] = ( is_array( $value ) ) ? $value : stripslashes( stripslashes( trim( $value ) ) );
				self::$pageVars[ 'GET' ][ $key ] = ( is_array( $value ) ) ? $value : stripslashes( stripslashes( trim( $value ) ) );
			}

			foreach( $_POST as $key => $value ) {
				//$value = ( is_array( $value ) ) ? $value : filter_var($value, FILTER_SANITIZE_STRING);
				
				self::$pageVars[ 'GETPOST' ][ $key ] = ( is_array( $value ) ) ? $value : stripslashes( stripslashes( trim( $value ) ) );
				self::$pageVars[ 'POST' ][ $key ] = ( is_array( $value ) ) ? $value : stripslashes( stripslashes( trim( $value ) ) );
			}
		}

		private static function checkFiles() {
			if( isset( $_SERVER[ 'HTTP_X_FILE_NAME' ] ) && isset( $_SERVER[ 'HTTP_X_FILE_SIZE' ] ) && isset( $_SERVER[ 'HTTP_X_FILE_TYPE' ] ) && isset( $_SERVER[ 'HTTP_X_FIELD_NAME' ] ) ) {
				$tempFile = tempnam( sys_get_temp_dir(), 'BOF' );
				$handle = fopen( $tempFile, 'w' );
				fwrite( $handle, file_get_contents( 'php://input' ) );
				fclose( $handle );
				chmod( $tempFile, 0777 );
				$fileType = explode( ';', $_SERVER[ 'HTTP_X_FILE_TYPE' ] != 'unknown file' ? $_SERVER[ 'HTTP_X_FILE_TYPE' ] : mime_content_type( $tempFile ) );
				$_FILES[ $_SERVER[ 'HTTP_X_FIELD_NAME' ] ] = array(
					'name' => $_SERVER[ 'HTTP_X_FILE_NAME' ],
					'type' => $fileType[ 0 ],
					'tmp_name' => $tempFile,
					'error' => 0,
					'size' => $_SERVER[ 'HTTP_X_FILE_SIZE' ]
				);
			}
		}
	}